import connection from '../app/database'
import { IRoleCreatParams, IRoleEditParams } from '@/types/roles.types'
import { IPagingQueryParams } from '@/types/index.type'

class RoleService {
  /**
   * 新增权限
   * @param role
   * @returns
   */
  public create(role: IRoleCreatParams) {
    const { roleName, remark } = role
    const statement: string = `INSERT INTO roles (roleName, remark) VALUES (?, ?);`
    return connection.execute(statement, [roleName, remark])
  }

  /**
   * 通过权限标识 获取权限信息
   * @param role
   * @returns
   */
  public getRoleByName(role: IRoleCreatParams) {
    const { roleName } = role
    const statement: string = `SELECT * from roles WHERE roleName = ?;`
    return connection.execute(statement, [roleName])
  }

  /**
   * 查看非自身以外的是否有同名权限（修改权限时，判断是否有同名权限）
   * @param role 角色model
   * @returns
   */
  public getOtherRoleByName(role: IRoleEditParams) {
    const { roleName, id } = role
    const statement: string = `SELECT * from roles WHERE roleName = ? AND id != ?;`
    return connection.execute(statement, [roleName, id])
  }

  /**
   * 修改权限
   * @param role
   * @returns
   */
  public edit(role: IRoleEditParams) {
    const { roleName, id, remark } = role
    const statement = `
        UPDATE roles SET
          roleName = ?,
          remark = ?
        WHERE id = ?;`
    return connection.execute(statement, [roleName, remark, String(id)])
  }

  /**
   * 删除权限
   * @param roleId
   * @returns
   */
  public delete(roleId: string) {
    const statement = `DELETE FROM roles WHERE id = ?`
    return connection.execute(statement, [roleId])
  }

  /**
   * 分页查询
   * @param params 分页以及权限信息
   * @returns
   */
  public getList(params: IPagingQueryParams) {
    const { pageNum, pageSize, remark, roleName } = params
    const offset = (Number(pageNum) - 1) * Number(pageSize)
    const arr = []
    let statement = `SELECT * FROM roles `
    if (remark || roleName) {
      if (remark && roleName) {
        statement += `WHERE roleName = ? AND remark LIKE '%${remark}%' ORDER BY updateTime DESC LIMIT ?, ?;`
        arr.push(roleName)
      } else if (remark) {
        statement += `WHERE remark LIKE '%${remark}%' ORDER BY updateTime DESC LIMIT ?, ?;`
      } else if (roleName) {
        statement += `WHERE roleName = ? ORDER BY updateTime DESC LIMIT ?, ?;`
        arr.push(roleName)
      }
    } else {
      statement += 'ORDER BY updateTime DESC LIMIT ?, ?;'
    }
    return connection.execute(statement, [...arr, String(offset), pageSize])
  }

  /**
   * count查询
   * @returns
   */

  public getCount() {
    const statement = 'SELECT COUNT(id) as count from roles;'
    return connection.execute(statement)
  }

  /**
   * 查看某一角色的菜单权限信息
   * @param roleId 角色ID
   * @returns
   */
  public getMenuByRole(roleId: string) {
    const statement = `
      SELECT
        p.id permissionId,
        R.id roleId,
        RF.id roleFermId,
        P.name menuName,
        P.pid pid,
        P.type type
      FROM
        roles R
      JOIN
        role_ferm RF
      ON
        R.id = RF.roleID AND R.id = ?
      RIGHT JOIN
        permissions P
      ON
        RF.permID = P.id;
      `
    return connection.execute(statement, [roleId])
  }

  /**
   * 删除某一角色的关联权限信息
   * @param roleId 角色ID
   * @returns
   */
  public delMenuByRole(roleId: string) {
    const statement = `DELETE FROM role_ferm WHERE roleID = ?;`
    return connection.execute(statement, [roleId])
  }

  /**
   * 配置角色权限关联信息
   * @param roleId number 角色id
   * @param permissions number[] 菜单权限id数组
   * @returns
   */
  public configPermissions(roleId: number, permissions: number[]) {
    let statement = 'INSERT INTO role_ferm(roleID, permID) VALUES'
    const len = permissions.length
    permissions.forEach((permission, index) => {
      statement += `(${roleId}, ${permission})${len - 1 > index ? ',' : ';'}`
    })
    console.log(statement)
    return connection.execute(statement)
  }
}

export default new RoleService()